// 封装分类数据业务相关代码
import { ref, onMounted } from "vue";
import { useRoute, onBeforeRouteUpdate } from "vue-router";
import { getCategoryAPI } from "@/apis/category";

export function useCategory() {
  // 获取分类数据
  const categoryData = ref({});
  const route = useRoute();
  // console.log(route.params.id)
  const getCategoryData = async (id = route.params.id) => {
    const res = await getCategoryAPI(id);
    categoryData.value = res.result;
    console.log(categoryData.value);
  };

  onBeforeRouteUpdate((to) => {
    getCategoryData(to.params.id);
  });

  onMounted(() => {
    getCategoryData();
    // getBanner()
  });

  return { categoryData };
}
